<!--
 * @Descripttion: 资产标签与目录
 * @Author: 颜博骏 bojun@ssc-hn.com
 * @Date: 2024-07-15
 * @LasteditCatalogors: 颜博骏 bojun@ssc-hn.com
 * @LasteditCatalogTime: 2024-07-16
-->
<script lang="ts" setup>
  import { ArrowLeftOutlined, PlusOutlined } from '@ant-design/icons-vue'
  import type { SelectProps } from 'ant-design-vue'
  import { onMounted, reactive, ref } from 'vue'
  import { watch } from 'vue'

  import { requestAddCatalog, requestAssetCatalogListByPage, requestSwitchCatalog } from '/@/api/business/asset/catalog'
  import { HttpCodeEnum } from '/@/enums/httpEnum'
  import { useQueryPagination } from '/@/hooks/useQueryPagination'

  import Configuration from './components/Configuration.vue'

  const searchParam = reactive({
    keyword: '',
    assetCatalogState: undefined,
    pid: '0',
  })
  const formState = reactive({
    assetCatalogName: '',
    assetCatalogDescribe: '',
  })
  const visible = ref<boolean>(false)
  const modalType = ref(0) // 0-新增 1-修改
  const rootName = ref('')
  const pageLevel = ref('0') // 0-根目录 1-子目录
  const { loading, queryList, dataList, pagination, onPaginationChange } = useQueryPagination<AssetCatalogTypes.IRequest, AssetCatalogTypes.IResponse[]>(searchParam, requestAssetCatalogListByPage)

  onMounted(() => {
    queryList()
  })
  watch(
    () => dataList.value,
    () => {
      console.log(dataList.value)
    },
  )
  const columns = [
    {
      title: '编号',
      dataIndex: 'assetCatalogNo',
      key: 'assetCatalogNo',
    },
    {
      title: '名称',
      key: 'assetCatalogName',
      dataIndex: 'assetCatalogName',
    },
    {
      title: '创建日期',
      dataIndex: 'createTime',
      key: 'createTime',
      sorter: (a, b) => a.createTime - b.createTime,
    },
    {
      title: '状态',
      key: 'assetCatalogState',
      dataIndex: 'assetCatalogState',
      customRender: ({ record }) => {
        return record.assetCatalogState === 1 ? '启用' : '禁用'
      },
    },
    {
      title: '操作',
      key: 'action',
    },
  ]
  const addCatalog = async () => {
    modalType.value = 1
    visible.value = true
  }
  const editCatalog = (item) => {
    modalType.value = 2
    visible.value = true
    formState.assetCatalogName = item.assetCatalogName
    formState.assetCatalogDescribe = item.assetCatalogDescribe
    if (pageLevel.value === '1') {
      rootName.value = item.assetCatalogName
    }
  }
  const updateState = async (item) => {
    const params = {
      id: item.id,
      assetCatalogState: item.assetCatalogState === 1 ? 2 : 1,
    }
    const { responseCode } = await requestSwitchCatalog(params)
    if (responseCode === HttpCodeEnum.SUCCESS) {
      queryList()
    }
  }
  const modalConfiguration = reactive({
    visible: false,
    companyId: NaN,
    success: () => queryList(),
  })
  const subCatalog = (record) => {
    rootName.value = record.assetCatalogName
    pageLevel.value = '1'
    searchParam.pid = record.id
    queryList({ current: 1 })
  }

  const backToRoot = () => {
    pageLevel.value = '0'
    searchParam.pid = '0'
    searchParam.assetCatalogState = undefined
    searchParam.keyword = ''
    queryList({ current: 1 })
  }
  const handleOk = async () => {
    const params = {
      assetCatalogName: formState.assetCatalogName,
      assetCatalogDescribe: formState.assetCatalogDescribe,
      pid: pageLevel.value === '0' ? '0' : searchParam.pid,
    }
    const { responseCode } = await requestAddCatalog(params)
    if (responseCode === HttpCodeEnum.SUCCESS) {
      visible.value = false
      queryList()
    }
  }
  const options3 = ref<SelectProps['options']>([
    {
      value: 1,
      label: '启用',
    },
    {
      value: 2,
      label: '禁用',
    },
  ])
  const reset = () => {
    searchParam.keyword = ''
    searchParam.assetCatalogState = undefined
    queryList({ current: 1 })
  }
  const layout = {
    labelCol: { span: 4 },
    wrapperCol: { span: 16 },
  }
</script>

<template>
  <div class="org-list">
    <div class="org-list-header">
      <div class="add-header" v-if="pageLevel !== '0'">
        <arrow-left-outlined :style="{ 'font-weight': 'bold', 'margin-right': '18px' }" @click="backToRoot" />
        <span>{{ rootName ? rootName : '子目录' }}</span>
      </div>
      <div class="fliter-wrap">
        <div>
          <div class="filter-item">
            <span class="filter-name">资产编号/名称：</span>
            <a-input v-model:value="searchParam.keyword" allowClear style="width: 300px" placeholder="模糊查询名称（精准查询编号）" @press-enter="queryList()" />
          </div>
          <div class="filter-item">
            <span class="filter-name">资产状态：</span>
            <a-select v-model:value="searchParam.assetCatalogState" allowClear :options="options3" style="width: 300px" placeholder="资产状态" />
          </div>
        </div>
        <div>
          <a-button class="filter-action" @click="reset">重置</a-button>
          <a-button class="filter-action" type="primary" @click="queryList({ current: 1 })">查询</a-button>
        </div>
      </div>
    </div>
    <div class="org-list-content">
      <div class="content-name">
        目录与标签
        <a-button type="primary" @click="addCatalog"><plus-outlined :style="{ 'font-weight': 'bold' }" />新增</a-button>
      </div>
      <a-table :columns="columns" :data-source="dataList" :loading="loading" :pagination="pagination" @change="onPaginationChange">
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'createTime'">
            {{ new Date(+record.createTime).toLocaleString() }}
          </template>
          <template v-else-if="column.key === 'action'">
            <span>
              <a @click="subCatalog(record)"> 子目录 </a>
              <a @click="editCatalog(record)">编辑</a>
              <a @click="updateState(record)"> 变更状态 </a>
            </span>
          </template>
        </template>
      </a-table>
    </div>
    <div class="modals">
      <a-modal v-model:visible="visible" :title="modalType === 1 ? '新增目录' : '编辑目录'" @ok="handleOk">
        <!-- <div class="fatherName" v-if="pageLevel === '1'">{{ rootName }}</div> -->
        <a-form :model="formState" v-bind="layout" name="nest-messages">
          <a-form-item label="一级目录" v-if="pageLevel === '1'">
            {{ rootName }}
          </a-form-item>
          <a-form-item name="assetCatalogName" label="名称" :rules="[{ required: true, message: '请输入目录名称' }]">
            <a-input v-model:value="formState.assetCatalogName" />
          </a-form-item>
          <a-form-item name="assetCatalogDescribe" label="描述">
            <a-textarea v-model:value="formState.assetCatalogDescribe" />
          </a-form-item>
        </a-form>
      </a-modal>
    </div>
    <Configuration v-model:visible="modalConfiguration.visible" :company-id="modalConfiguration.companyId" @success="modalConfiguration.success" />
  </div>
</template>

<style lang="less" scoped>
  .org-list {
    .org-list-header {
      background-color: #fff;
      .add-header {
        font-size: 18px;
        font-weight: 500;
        color: #000;
        padding: 22px 0 0 26px;
      }
      .fliter-wrap {
        border-radius: 2px;
        background: #fff;
        margin-bottom: 16px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 24px;

        .filter-item {
          display: inline-block;

          &:not(:nth-last-of-type(1)) {
            margin-right: 44px;
          }

          .filter-name {
            color: #000;
          }
        }

        .filter-action {
          &:not(:nth-last-of-type(1)) {
            margin-right: 8px;
          }
        }
      }
    }

    .org-list-content {
      border-radius: 2px;
      background: #fff;
      padding: 20px 24px;

      .content-name {
        font-size: 16px;
        font-weight: 500;
        color: #000;
        margin-bottom: 20px;
      }
    }

    .content-name {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
  }
</style>
